package com.sample.javabean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;




import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sample.action.form.JianchaForm;
import com.sample.jdbc.JdbcUtils;
import com.sample.util.Pager;


/**
 * 日常体检信息类
 * @author梁桂花
 *
 * 2018-10-26
 */
public class Jiancha {
	
	private static Log LOG =LogFactory.getLog(Jiancha.class);
	//定义属性
	private String	className	;
	private String	tctime	;
	private String	ccheck;
	private String	nname	;
	private String	zzhuang	;
	private String	deal	;

	
	private JdbcUtils jdbcUtils ;

	public Jiancha(){
		jdbcUtils=new JdbcUtils();
	}
	
	/**
	 * 日常体检信息列表
	 * @return
	 */
	public List<Jiancha> findAll(){
		Connection conn=null;
		Statement stmt =null;
		ResultSet rs =null;
		List<Jiancha> jianchaList =new ArrayList<Jiancha>(20);
		try{
			 conn =jdbcUtils.getConn();
			 stmt =conn.createStatement();
			//构造插入sql
			String sql="select * from t_jiancha where 1=1";
			 rs =stmt.executeQuery(sql);
			while(rs.next()){
				String className=rs.getString("className");
				String tctime =rs.getString("tctime");
			    String	ccheck=rs.getString("ccheck");
			    String	nname=rs.getString("nname");
			    String	zzhuang=rs.getString("zzhuang")	;
				String	deal=rs.getString("deal");	
				 Jiancha model=new Jiancha(className,tctime,ccheck,nname,zzhuang,deal);
				
				jianchaList.add(model);
							}
			
		}catch (Exception e) {
			e.printStackTrace();
			LOG.error("加载体检信息失败!", e);
		}finally{
			jdbcUtils.close(conn, stmt,rs);
		}
		return jianchaList;
		
	}
	
     public void load()throws Exception{
 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null; 
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			
 			String sql = "select * from  t_jiancha where tctime='"+this.tctime+"'";
 			
 			rs=stmt.executeQuery(sql);
 			if(rs.next()){
 				this.className =rs.getString("className");
 				this.tctime =rs.getString("tctime");
 				this.ccheck=rs.getString("ccheck");
 			  	this.nname=rs.getString("nname");
 			   	this.zzhuang=rs.getString("zzhuang");
 				this.deal=rs.getString("deal")	;				
 			}
 			
 		} catch (SQLException e) {
 			e.printStackTrace();
 			throw new SQLException("加载信息");
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}	
 	}
 	public void editJiancha() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			String sql = "update  t_jiancha set className='"+this.className+"',tctime='"+this.tctime+"',ccheck='"+this.ccheck+"',nname='"+this.nname+"',zzhuang='"+this.zzhuang+"',deal='"+this.deal+"'where tctime='"+this.tctime+"'";
 			stmt.executeUpdate(sql);
 		
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void delJiancha() {

 		Connection conn = null;
 		Statement stmt = null;
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 		
 			String sql = "delete from t_jiancha  where tctime='"+this.tctime+"'";
 			
 			System.out.println("删除体检信息");
 			stmt.executeUpdate(sql);
 			
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}

 	}
 	public void add(){
 		Connection conn =null;
 		Statement stmt=null;
 		try{
 				 conn =jdbcUtils.getConn();
 			String sql="insert into t_jiancha(className,tctime,ccheck,nname,zzhuang,deal) values('"+this.className+"','"+this.tctime+"','"+this.ccheck+"','"+this.nname+"','"+this.zzhuang+"','"+this.deal+"')";
 			 stmt =conn.createStatement();
 			stmt.executeUpdate(sql);
 		}catch (Exception e) {
 			e.printStackTrace();
 			
 		}finally{
 			jdbcUtils.close(conn, stmt);
 			
 		}
 		
 	}
 	public Pager<Jiancha> list(int page,int size,JianchaForm form) {

 		Connection conn = null;
 		Statement stmt = null;
 		ResultSet rs =null;
 		Pager<Jiancha> results=new Pager<Jiancha>();
 		results.setPage(page);
 		results.setSize(size);
 		try {
 			conn = jdbcUtils.getConn();
 			stmt = conn.createStatement();
 			int start=(page-1)*size;
 			int limit=size;
 			
 			String selectSql="select * from t_jiancha where 1=1";
 			if(form!=null&&form.getNname()!=null&&!"".equals(form.getNname())){
 				selectSql +=" and Nname like'%"+form.getNname()+"%'";
 			}
 			
 			//缁熼敓鏂ゆ嫹SQL
 			String countSql ="select count(*)as c from ("+selectSql+") as t1";
 			//閿熸枻鎷烽〉SQL
 			 String pageSql=selectSql+" limit "+start+","+limit;
 			 rs= stmt.executeQuery(countSql);
 			 int total=0;
 			 while(rs.next()){
 				 total=rs.getInt("c");
 			 }
 			 results.setTotal(total);
 			 //閿熸枻鎷疯閿熸枻鎷烽〉閿熸枻鎷锋伅
 			 rs= stmt.executeQuery(pageSql);
 			 while(rs.next()){
 			 
 				String className=rs.getString("className");
				String tctime =rs.getString("tctime");
			    String	ccheck=rs.getString("ccheck");
			    String	nname=rs.getString("nname");
			    String	zzhuang=rs.getString("zzhuang")	;
				String	deal=rs.getString("deal");
 				
 				 Jiancha model=new Jiancha();
 				  model.setClassName(className);
 				  model.setTctime(tctime);
 				  model.setCcheck(ccheck);
 				  model.setNname(nname);
 				  model.setZzhuang(zzhuang);
 				  model.setDeal(deal);
 				 
 		
 				  results.getDataSets().add(model);
 			 }
 			 results.init();
 			// 5:閿熸埅鎲嬫嫹閿熸枻鎷锋簮
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} finally {
 			jdbcUtils.close(conn, stmt);
 		}
 		return results;

 	}
 	

	public Jiancha( String className, String tctime, String ccheck,String nname,
			String zzhuang,String deal ) {
		super();
		
		this.className =className;
		this.tctime = tctime;
		this.ccheck=ccheck;
		this.nname=nname;
		this.zzhuang=zzhuang;
	    this.deal=deal;
		
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getTctime() {
		return tctime;
	}

	public void setTctime(String tctime) {
		this.tctime = tctime;
	}

	public String getCcheck() {
		return ccheck;
	}

	public void setCcheck(String ccheck) {
		this.ccheck = ccheck;
	}

	public String getNname() {
		return nname;
	}

	public void setNname(String nname) {
		this.nname = nname;
	}

	public String getZzhuang() {
		return zzhuang;
	}

	public void setZzhuang(String zzhuang) {
		this.zzhuang = zzhuang;
	}

	public String getDeal() {
		return deal;
	}

	public void setDeal(String deal) {
		this.deal = deal;
	}

	public JdbcUtils getJdbcUtils() {
		return jdbcUtils;
	}

	public void setJdbcUtils(JdbcUtils jdbcUtils) {
		this.jdbcUtils = jdbcUtils;
	}

	

	
	
	
}
